package com.shfl.servermanage.logic.api.bt;

import cn.hutool.json.JSONUtil;
import com.shfl.servermanage.bt.BtComponent;
import com.shfl.servermanage.bt.BtConstants;
import com.shfl.servermanage.common.ApiResultListHelper;
import com.shfl.servermanage.common.SystemConstant;
import com.shfl.servermanage.logic.model.bt.response.PhpVersionResponse;
import com.shfl.servermanage.logic.model.info.BtUserInfo;
import com.shfl.servermanage.logic.service.SysServerBtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/bt/php")
@Api(value = "站点", description = "站点")
public class BtPhpController {

    @Autowired
    private BtComponent btComponent;

    @Autowired
    private SysServerBtService sysServerBtService;

    @GetMapping(value = "/version")
    @ApiOperation(value = "PHP版本", notes = "PHP版本")
    public ApiResultListHelper<PhpVersionResponse> phpVersion(
            @ApiParam(name = "serverId", example = "1", value = "服务器Id", required = true)
            @RequestParam(value = "serverId") Long serverId
    ) throws Exception {
        BtUserInfo info = sysServerBtService.findBtUserInfo(serverId);
        info.setApiUrl(BtConstants.PHP_VERSION);
        String resultString = btComponent.send(info, null);
        List<PhpVersionResponse> resultList = JSONUtil.toList(resultString, PhpVersionResponse.class);
        return new ApiResultListHelper<>(SystemConstant.RE_SUCCESS_CODE, resultList);
    }
}
